﻿// JavaScript Document
var dic = null;
function meaningWindow(meaning){ //desplay the meaning in a iframe window
		var meaningWindow=window.open('',"wordMeaning");
		meaningWindow.document.write(meaning);
		meaningWindow.document.close();
	}
function listWindow(list){ //desplay the meaning in a iframe window
		var listWindow=window.open('',"wordList");
		meaningWindow.document.write(list);
		listWindow.document.close();
	}
function loadXml() {
        $.ajax({  
            url: './word.xml',  
            type: 'GET',  
            dataType: 'xml',  
            timeout: 1000,  //设定超时  
            cache: false,   //禁用缓存  
            error: function() {  
               alert("Error Loading XML!");   
            },  
            success: function(xml) { //设置成功后回调函数  
				dic = $(xml);
			}  
        });    
}
function list(wordInq) {
	wordInq = wordInq.toLowerCase();
	var wordPat = /[a-z]*/;
		dic.find("item").each(function() {     //查找所有item节点并遍历  
			var word_vaule = $(this).children("word").text();			//获得子节点值  
			var word_vaule_lower = $(this).children("word").text().toLowerCase();	
			if ( wordPat.test(word_vaule_lower)) {
				listWindow("<p>word_vaule</p>")
			}
});
}
function inquire(wordInq) {
	var w = null;
	var flag = true;
	wordInq = wordInq.toLowerCase();
		dic.find("item").each(function() {     //查找所有item节点并遍历  
			var word_vaule = $(this).children("word").text();			//获得子节点值  
			var word_vaule_lower = $(this).children("word").text().toLowerCase();	
			if ( word_vaule_lower == wordInq) {
				w = $(this);
				var m=w.children("trans").text(); 
				var i=w.children("phonetic").text(); 
				var g=w.children("tags").text();
				meaningWindow("<br /><span style='color:red; font-weight:bold;'>单词：</span>"+ word_vaule +"<br /><span style='color:red; font-weight:bold;'>解释：</span>"+m+"<br /><span style='color:red; font-weight:bold;'>音标：</span>"+i+"<br /><span style='color:red; font-weight:bold;'>等级：</span>"+g);//despaly the meaning of the word
				flag = false;
			}
		});
		if (flag) {
				meaningWindow("<br />目前还没有您要查的单词哦");//if the word is not in word.xml
			}
}
function keyUp(evt){
	evt = evt||window.event;
	var key = evt.keyCode||evt.which||evt.charCode;
	if (key==13){
		document.getElementById("inq").click();
		evt.returnValue=false;
	}
}
document.onkeyup = keyUp;